package com.qingmu.club.minaapi.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.plus.entity.Club;
import com.qingmu.club.minaapi.plus.entity.ClubStaff;
import com.qingmu.club.minaapi.plus.entity.ClubStaffShop;
import com.qingmu.club.minaapi.plus.service.ClubService;
import com.qingmu.club.minaapi.plus.service.ClubStaffService;
import com.qingmu.club.minaapi.plus.service.ClubStaffShopService;
import com.qingmu.club.minaapi.utils.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 俱乐部主表 前端控制器
 * </p>
 *
 * @author duke
 * @since 2019-08-20
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(value = "俱乐部相关(会员端小程序)", description = "俱乐部相关(会员端小程序)")
@RequestMapping("/member/club")
public class MemberClubController {

    @Autowired
    private ClubService clubService;

    @Autowired
    private ClubStaffService clubStaffService;

    @Autowired
    private ClubStaffShopService clubStaffShopService;

    @RequestMapping(value = "/getById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询俱乐部详情")
    public ResponseEntity getById(
            @ApiParam(required = true, value = "俱乐部id") @RequestParam(required = true) String clubId
    ){
        Club club=this.clubService.getById(clubId);

        if(club==null){
            return ResponseObject.error(601,"俱乐部不存在");
        }
        return ResponseObject.success(club);
    }


    @RequestMapping(value = "/getCoach", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询俱乐部门店教练")
    public ResponseEntity queryClubCoach(
            @ApiParam(required = true, value = "俱乐部id") @RequestParam(required = true) String clubId,
            @ApiParam(required = false, value = "门店id") @RequestParam(required = false) String shopId
    ){

        List<ClubStaff> clubStaffList = new ArrayList<>();

        if(!StringUtils.isEmpty(shopId)) {
            QueryWrapper<ClubStaffShop> shopQueryWrapper = new QueryWrapper<>();
            shopQueryWrapper.eq(ClubStaffShop.SHOP_ID, shopId).eq(ClubStaffShop.STAFF_TAG, 0);
            List<ClubStaffShop> clubStaffShopList = this.clubStaffShopService.list(shopQueryWrapper);
            if(!CollectionUtils.isEmpty(clubStaffShopList)){
                for(ClubStaffShop clubStaffShop : clubStaffShopList){
                    ClubStaff clubStaff =this.clubStaffService.getById(clubStaffShop.getStaffId());
                    clubStaffList.add(clubStaff);
                }
            }
        }else {

            QueryWrapper<ClubStaff> qw = new QueryWrapper<>();
            qw.eq(ClubStaff.CLUB_ID, clubId).eq(ClubStaff.IS_COACH, 1);
            clubStaffList = this.clubStaffService.list(qw);
        }
        return ResponseObject.success(clubStaffList);
    }

}

